
#region Imported Namespaces
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Web.UI;
using Utility = PHSRAG.Utility;
#endregion

namespace PHSRAG.WebControls
{
	/// <summary>
	/// The Helper class defines static helper methods.
	/// </summary>
	public class Helper
	{
		/// <summary>String used in prompting a use to select something</summary>
		public const string SelectPrompt = "-- Select --";
        private const string DecimalPointKey = "DecimalPoint";

		/// <summary>
		/// Get the web application root.
		/// </summary>
		/// <param name="page">Reference to a page within the web application</param>
		/// <returns>Application path</returns>
		public static string GetApplicationPath(Page page)
		{

            return "..";
         
		}

		/// <summary>
		/// Get the MIME type based on the specified file extension.
		/// </summary>
		/// <param name="fileExtension">File extension</param>
		/// <returns>MIME type for known extension, null otherwise</returns>
		public static string GetMimeType(string fileExtension)
		{
			switch (fileExtension.ToLower())
			{
				case ".ez":
					return "application/andrew-inset";
				case ".hqx":
					return "application/mac-binhex40";
				case ".cpt":
					return "application/mac-compactpro";
				case ".mathml":
					return "application/mathml+xml";
				case ".doc":
					return "application/msword";
				case ".bin":
				case ".dms":
				case ".lha":
				case ".lzh":
				case ".exe":
				case ".class":
				case ".so":
				case ".dll":
					return "application/octet-stream";
				case ".oda":
					return "application/oda";
				case ".ogg":
					return "application/ogg";
				case ".pdf":
					return "application/pdf";
				case ".ai":
				case ".eps":
				case ".ps":
					return "application/postscript";
				case ".rdf":
					return "application/rdf+xml";
				case ".smi":
				case ".smil":
					return "application/smil";
				case ".gram":
					return "application/srgs";
				case ".grxml":
					return "application/srgs+xml";
				case ".mif":
					return "application/vnd.mif";
				case ".xul":
					return "application/vnd.mozilla.xul+xml";
				case ".xls":
					return "application/vnd.ms-excel";
				case ".ppt":
					return "application/vnd.ms-powerpoint";
				case ".wbxml":
					return "application/vnd.wap.wbxml";
				case ".wmlc":
					return "application/vnd.wap.wmlc";
				case ".wmlsc":
					return "application/vnd.wap.wmlscriptc";
				case ".vxml":
					return "application/voicexml+xml";
				case ".bcpio":
					return "application/x-bcpio";
				case ".vcd": 
					return "application/x-cdlink";
				case ".pgn": 
					return "application/x-chess-pgn";
				case ".cpio": 
					return "application/x-cpio";
				case ".csh": 
					return "application/x-csh";
				case ".dcr":
				case ".dir":
				case ".dxr":
					return "application/x-director";
				case ".dvi":
					return "application/x-dvi";
				case ".spl":
					return "application/x-futuresplash";
				case ".gtar":
					return "application/x-gtar";
				case ".hdf":
					return "application/x-hdf";
				case ".php":
				case ".php4":
				case ".php3":
				case ".phtml":
					return "application/x-httpd-php";
				case ".phps":
					return "application/x-httpd-php-source";
				case ".js": 
					return "application/x-javascript";
				case ".skp":
				case ".skd":
				case ".skt":
				case ".skm":
					return "application/x-koan";
				case ".latex": 
					return "application/x-latex";
				case ".nc":
				case ".cdf": 
					return "application/x-netcdf";
				case ".crl":
					return "application/x-pkcs7-crl";
				case ".sh": 
					return "application/x-sh";
				case ".shar": 
					return "application/x-shar";
				case ".swf":
					return "application/x-shockwave-flash";
				case ".sit": 
					return "application/x-stuffit";
				case ".sv4cpio": 
					return "application/x-sv4cpio";
				case ".sv4crc": 
					return "application/x-sv4crc";
				case ".tgz":
				case ".tar":
					return "application/x-tar";
				case ".tcl":
					return "application/x-tcl";
				case ".tex":
					return "application/x-tex";
				case ".texinfo":
				case ".texi":
					return "application/x-texinfo";
				case ".t":
				case ".tr":
				case ".roff":
					return "application/x-troff";
				case ".man":
					return "application/x-troff-man";
				case ".me":
					return "application/x-troff-me";
				case ".ms":
					return "application/x-troff-ms";
				case ".ustar":
					return "application/x-ustar";
				case ".src":
					return "application/x-wais-source";
				case ".crt":
					return "application/x-x509-ca-cert";
				case ".xht":
					return "application/xhtml+xml xhtml";
				case ".xml":
				case ".xsl":
					return "application/xml";
				case ".dtd":
					return "application/xml-dtd";
				case ".xslt":
					return "application/xslt+xml";
				case ".zip":
					return "application/zip";
				case ".au":
				case ".snd":
					return "audio/basic";
				case ".mid":
				case ".midi":
				case ".kar":
					return "audio/midi";
				case ".mpga":
				case ".mp2":
				case ".mp3":
					return "audio/mpeg";
				case ".aif":
				case ".aiff":
				case ".aifc":
					return "audio/x-aiff";
				case ".m3u":
					return "audio/x-mpegurl";
				case ".ram":
				case ".rm":
					return "audio/x-pn-realaudio";
				case ".rpm":
					return "audio/x-pn-realaudio-plugin";
				case ".ra":
					return "audio/x-realaudio";
				case ".wav":
					return "audio/x-wav";
				case ".pdb":
					return "chemical/x-pdb";
				case ".xyz":
					return "chemical/x-xyz";
				case ".bmp":
					return "image/bmp";
				case ".cgm":
					return "image/cgm";
				case ".gif":
					return "image/gif";
				case ".ief":
					return "image/ief";
				case ".jpeg jpg jpe":
					return "image/jpeg";
				case ".png":
					return "image/png";
				case ".svg":
					return "image/svg+xml";
				case ".tiff":
				case ".tif":
					return "image/tiff";
				case ".djvu":
				case ".djv":
					return "image/vnd.djvu";
				case ".wbmp":
					return "image/vnd.wap.wbmp";
				case ".ras":
					return "image/x-cmu-raster";
				case ".ico":
					return "image/x-icon";
				case ".pnm":
					return "image/x-portable-anymap";
				case ".pbm":
					return "image/x-portable-bitmap";
				case ".pgm":
					return "image/x-portable-graymap";
				case ".ppm":
					return "image/x-portable-pixmap";
				case ".rgb":
					return "image/x-rgb";
				case ".xbm":
					return "image/x-xbitmap";
				case ".xpm":
					return "image/x-xpixmap";
				case ".xwd":
					return "image/x-xwindowdump";
				case ".iges":
					return "model/iges igs";
				case ".msh":
				case ".mesh":
				case ".silo":
					return "model/mesh";
				case ".wrl":
				case ".vrml":
					return "model/vrml";
				case ".ics":
				case ".ifb":
					return "text/calendar";
				case ".css":
					return "text/css";
				case ".shtml":
				case ".html":
				case ".htm":
				case ".chm":
					return "text/html";
				case ".log":
				case ".sql":
				case ".asc":
				case ".txt":
				case ".vbs":
				case ".bat":
				case ".cs":
				case ".vb":
					return "text/plain";
				case ".rtx":
					return "text/richtext";
				case ".rtf":
					return "text/rtf";
				case ".sgml":
				case ".sgm":
					return "text/sgml";
				case ".tsv":
					return "text/tab-separated-values";
				case ".wml":
					return "text/vnd.wap.wml";
				case ".wmls":
					return "text/vnd.wap.wmlscript";
				case ".etx":
					return "text/x-setext";
				case ".mpeg":
				case ".mpg":
				case ".mpe":
					return "video/mpeg";
				case ".qt":
				case ".mov":
					return "video/quicktime";
				case ".mxu":
					return "video/vnd.mpegurl";
				case ".avi":
					return "video/x-msvideo";
				case ".movie":
					return "video/x-sgi-movie";
				case ".ice":
					return "x-conference/x-cooltalk";
			}
			return null;
		}

		/// <summary>
		/// Allows server controls to automatically register a hidden field on the form. 
		/// The field will be sent to the Page when the HtmlForm server control is rendered.
		/// The input element will include both a "name" and "id" element to support IE
		/// as well as other browser types/versions that rely on the id attribute in order 
		/// for getElementById to return a value.
		/// </summary>
		/// <param name="page">The page that the hidden field is to be registered on.</param>
		/// <param name="hiddenFieldName">The unique name of the hidden field to be rendered.</param>
		/// <param name="hiddenFieldValue">The value to be emitted in the hidden form.</param>
		/// 
		public static void RegisterHiddenFieldWithId(Page page, string hiddenFieldName, string hiddenFieldValue)
		{
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), hiddenFieldName, 
				String.Format("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\"/>\n" ,hiddenFieldName, hiddenFieldValue));
		}

		/// <summary>
		/// Provide NumberFormatInfo object that specifies no decimal places
		/// </summary>
		/// <returns></returns>
		public static NumberFormatInfo ZeroDecimals()
		{
			NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
			nfi.CurrencyDecimalDigits = 0;
			return nfi;
		}

		/// <summary>
		/// Provide NumberFormatInfo object that specifies passed in number of decimals
		/// </summary>
		/// <returns></returns>
		public static NumberFormatInfo ChooseDecimals(int numberDecimals)
		{

			NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
			nfi.CurrencyDecimalDigits = numberDecimals;
			return nfi;
		}

        /// <summary>
		/// return value by applying rounding rule
		/// </summary>
		/// <returns></returns>
        public static string ProcessDecimals(decimal numberDecimals)
        {
            int NumDecimal = Utility.Settings.GetKeyValue(DecimalPointKey, 0);

            return numberDecimals.ToString("c", ChooseDecimals(NumDecimal));
        }
	}
}

// --- EOF ---
